package com.djsearch.ui;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.djsearch.R;
import com.djsearch.bean.LvYouOrderBean;
import com.djsearch.bean.LvYouSendOrderBean;
import com.djsearch.controller.JSONResp;
import com.djsearch.data.LvYouDataRequest;

public class HandMyOrderActivity extends BaseActivity{
	
	private Context mContext;
	private Button backBtn;
	private EditText nameEdit,detailAddEdit,bianMaEidt,telEdit,giveInfoEidt,provinceEdit,cityEdit,townEdit;
	private TextView tijiao;
	private LvYouSendOrderBean lvYouSendOrderBean;//订单实体类
	private LvYouOrderBean lvYouOrderBean;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mContext = this;
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(
				WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
		setContentView(R.layout.hand_myorder_layout);
		lvYouOrderBean = (LvYouOrderBean) getIntent().getSerializableExtra("data");
		lvYouSendOrderBean = new LvYouSendOrderBean();
		initView();
		initOnClick();
	}

	@Override
	void initHandler() {
		// TODO Auto-generated method stub
		
	}

	
	@Override
	void initView() {
		// TODO Auto-generated method stub
		backBtn = (Button)findViewById(R.id.btnBack);
		nameEdit = (EditText)findViewById(R.id.nameEdit);
		detailAddEdit = (EditText)findViewById(R.id.detailAddEdit);
		bianMaEidt = (EditText)findViewById(R.id.bianMaEidt);
		telEdit = (EditText)findViewById(R.id.telEdit);
		giveInfoEidt = (EditText)findViewById(R.id.giveInfoEidt);
		tijiao = (TextView)findViewById(R.id.tijiao);
		provinceEdit = (EditText)findViewById(R.id.province);
		cityEdit = (EditText)findViewById(R.id.city);
		townEdit = (EditText)findViewById(R.id.town);
	}

	@Override
	void initOnClick() {
		// TODO Auto-generated method stub
		backBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		/**
		 * 提交订单
		 */
		tijiao.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				creatLvYouSendOrderBean();
				sendOrderData(lvYouSendOrderBean);
			}
		});
		
	}

	
	/**
	 * 生成订单信息
	 * cart    购物车数据(拼接逻辑：商品编号|1为成人2为儿童|数量|订购日期  
     * 为一个商品数据，两个商品数据间用,号分隔
	 */
	private void creatLvYouSendOrderBean() {
		if (lvYouSendOrderBean != null) {
			lvYouSendOrderBean.setOrder_DataUserId(preferences.getString("userId", ""));//用户账号
			lvYouSendOrderBean.setOrder_DataTo(nameEdit.getText().toString());//收货人姓名
			lvYouSendOrderBean.setOrder_DataAddr(provinceEdit.getText().toString() + cityEdit.getText().toString() + townEdit.getText().toString() + detailAddEdit.getText().toString());//收货人地址
			lvYouSendOrderBean.setOrder_DataZip(bianMaEidt.getText().toString());//收货邮编
			lvYouSendOrderBean.setOrder_DataPhone(telEdit.getText().toString());//联系电话
			lvYouSendOrderBean.setOrder_DataAbout(giveInfoEidt.getText().toString());//订单留言
			lvYouSendOrderBean.setOrder_DataYunFei(lvYouOrderBean.getOrder_DataYunFei());
			lvYouSendOrderBean.setOrder_DataPay(lvYouOrderBean.getOrder_DataPay());
			lvYouSendOrderBean.setOrder_DataSp(lvYouOrderBean.getTotalPrice());
			lvYouSendOrderBean.setCart(lvYouOrderBean.getGoodsId() + "|" + lvYouOrderBean.getNumber() + "|" + lvYouOrderBean.getDate());
		}
		
	}
	
	
	/**
	 * 发送订单
	 */
	private void sendOrderData(final LvYouSendOrderBean lvYouSendOrderBean) {
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				LvYouDataRequest.getIntence().requestWcf("CreateOrder",lvYouSendOrderBean);
			}
		}).start();
	}
	
	@Override
	void getDataToResolve(JSONResp response) {
		// TODO Auto-generated method stub
		
	}

}
